package com.zfzs.post.modules.transaction.service.impl;

import com.zfzs.post.modules.transaction.domain.BenefitInfo;
import com.zfzs.post.modules.transaction.repository.BenefitInfoRepository;
import com.zfzs.post.modules.transaction.service.BenefitInfoService;
import com.zfzs.post.modules.transaction.service.dto.BenefitInfoQueryCriteria;
import com.zfzs.post.modules.transaction.service.mapper.BenefitInfoMapper;
import com.zfzs.post.utils.PageUtil;
import com.zfzs.post.utils.QueryHelp;
import com.zfzs.post.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
* @author wq
* @date 2019-10-23
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BenefitInfoServiceImpl implements BenefitInfoService {

    @Autowired
    private BenefitInfoRepository benefitInfoRepository;

    @Autowired
    private BenefitInfoMapper benefitInfoMapper;

    @Override
    public Object queryAll(BenefitInfoQueryCriteria criteria, Pageable pageable){
        Page<BenefitInfo> page = benefitInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(benefitInfoMapper::toDto));
    }

    @Override
    public Object queryAll(BenefitInfoQueryCriteria criteria){
        return benefitInfoMapper.toDto(benefitInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public BenefitInfo findById(Long benefitInfoId) {
        Optional<BenefitInfo> benefitInfo = benefitInfoRepository.findById(benefitInfoId);
        ValidationUtil.isNull(benefitInfo,"BenefitInfo","benefitInfoId",benefitInfoId);
        return benefitInfo.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BenefitInfo create(BenefitInfo resources) {
        return benefitInfoRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BenefitInfo resources) {
        Optional<BenefitInfo> optionalBenefitInfo = benefitInfoRepository.findById(resources.getBenefitInfoId());
        ValidationUtil.isNull( optionalBenefitInfo,"BenefitInfo","id",resources.getBenefitInfoId());
        BenefitInfo benefitInfo = optionalBenefitInfo.get();
        benefitInfo.copy(resources);
        benefitInfoRepository.save(benefitInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long benefitInfoId) {
        benefitInfoRepository.deleteById(benefitInfoId);
    }

    @Override
    public BenefitInfo findByBenefitId(Long benefitId, Long terminalTypeId) {
        return benefitInfoRepository.findByBenefitIdAndTerminalTypeId(benefitId, terminalTypeId);
    }

    @Override
    public List<BenefitInfo> findByBenefitId(Long benefitId) {
        return benefitInfoRepository.findByBenefitId(benefitId);
    }
}